﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AutoPublishWebSite
{
    class Program
    {
        static void Main(string[] args)
        {
            var port = System.Configuration.ConfigurationSettings.AppSettings["port"];
            Console.WriteLine("web服务启动:" + port);
            new HttpServer().StartWebServer(Convert.ToInt32(port), (string url, string data) =>
            {
                try
                {
                    var urlParams = Util.ParseQueryString(url);
                    var uri = new Uri(url);
                    var route = uri.LocalPath.Substring(1, uri.LocalPath.Length - 1);

                    switch (url)
                    {
                        //getFilesSHA1
                        case string a when route.StartsWith("getFilesSHA1"):
                            {
                                if (!urlParams.ContainsKey("path"))
                                {
                                    return Error($"{a}:缺少参数path！");
                                }
                                var path = urlParams["path"].ToString();
                                return Success(Util.GetAllFilesSHA1(path));
                            };
                    }
                    return $"{url}--{data}\n\rhelloworld";
                }
                catch (Exception ex)
                {
                    return Error(ex.Message + "\n\r" + ex.StackTrace);
                }
            });
            Console.ReadKey();
        }

        static string Success(object data)
        {
            return JsonConvert.SerializeObject(new ReturnMsg { issucess = true, msg = "操作成功!", data = data });
        }
        static string Error(string msg)
        {
            return JsonConvert.SerializeObject(new ReturnMsg { issucess = false, msg = $"操作失败！{msg}" });
        }
    }
}
